﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;

namespace Client_Revenue
{
    public partial class AddCarriers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                pnContactAddres.Visible = false;
            chkSameAddress.Checked = true;
        }

        protected void btAddCarrier_Click(object sender, EventArgs e)
        {

            try
            {
                if (Page.IsValid)
                {
                    using (var DE = new dominiumEntities())
                    {
                        //Save the Carriers's details
                        ObjectParameter sAddressId = new ObjectParameter("AddressId", typeof(int));
                        ObjectParameter pAddressId = new ObjectParameter("AddressId", typeof(int));
                        ObjectParameter sCarrierId = new ObjectParameter("CarrierId", typeof(int));
                        ObjectParameter sPersonId = new ObjectParameter("PersonId", typeof(int));

                        if (chkSameAddress.Checked)
                        {
                            //save only carrier's address
                            DE.AddAddress(txtLine1.Text, txtLine2.Text, txtLine3.Text, txtCity.Text, txtState.Text, txtZip.Text, txtURL.Text, sAddressId);
                            DE.AddPerson(txtFirstName.Text, txtLastName.Text, txtMid.Text, txtTitle.Text, txtEmail.Text, txtWorkPhone.Text, txtMobile.Text, Convert.ToInt32(sAddressId.Value), sPersonId);
                        }
                        else //save both
                        {
                            //contact address
                            DE.AddAddress(txtLine1.Text, txtLine2.Text, txtLine3.Text, txtCity.Text, txtState.Text, txtZip.Text, txtURL.Text, sAddressId);

                            //Carrier's address
                            DE.AddAddress(txtContactAddress1.Text, txtContactAddress2.Text, txtContactAddress3.Text, txtContactCity.Text, txtContactState.Text, txtContactZip.Text, "n/a", pAddressId);
                            DE.AddPerson(txtFirstName.Text, txtLastName.Text, txtMid.Text, txtTitle.Text, txtEmail.Text, txtWorkPhone.Text, txtMobile.Text, Convert.ToInt32(pAddressId.Value), sPersonId);
                        }
                        DE.AddCarrier(txtCarrierName.Text, Convert.ToInt32(sAddressId.Value), sCarrierId);
                        DE.AddContactPerson(Convert.ToInt32(sCarrierId.Value), chkIsPrimary.Checked, false, Convert.ToInt32(sPersonId.Value));

                        ResetFormControlValues(this);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected void ckSameAddress_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkSameAddress.Checked)
            { pnContactAddres.Visible = true; }
            else
            { pnContactAddres.Visible = false; }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFormControlValues(this);
        }

        private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    ResetFormControlValues(c);
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":
                            ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            if (((CheckBox)c).Checked == false)
                            { ((CheckBox)c).Checked = true; }
                            else
                            { ((CheckBox)c).Checked = false; }

                            break;
                        case "System.Web.UI.WebControls.RadioButton":
                            ((RadioButton)c).Checked = false;
                            break;

                    }
                }
            }
        }


    }
}